
var fft = new Tone.FFT(32);
//get the waveform data for the audio
var waveform = new Tone.Waveform(1024);
var player = new Tone.Player({
  "url": "kids.[m4a]",
  "loop": false
}).fan(fft, waveform).toMaster();

window.onclick = function () {
  player.start()
  loop();
}
var count = 0
var xhr = new XMLHttpRequest();
function loop() {
  let id = requestAnimationFrame(loop);
  //get the fft data and draw it
  if (player.state == 'started') {
    var fftValues = fft.getValue();
    xhr.open("POST", '/data', true);
    xhr.setRequestHeader('Content-Type', 'text/html');
    let key = count.toString()
    count++
    // console.log(fftValues.toString())
    xhr.send('\"' + key + '\": \"' + fftValues.toString() + '\",');
  }

  if (player.state == "stopped") {
    console.log(count)
    xhr.open("GET", '/done', true);
    xhr.send()
    cancelAnimationFrame(id)
  }
  // //get the waveform valeus and draw it
  // var waveformValues = waveform.getValue();
  // drawWaveform(waveformValues);
}
